package com.moji.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.server.HttpServerRequest;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.moji.common.utils.DateUtils;
import com.moji.entity.MjStatistics;
import com.moji.entity.MjVisitRecord;
import com.moji.mapper.MjStatisticsMapper;
import com.moji.service.MjStatisticsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moji.service.MjVisitRecordService;
import com.moji.util.MoJiCommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 * 数据统计表 服务实现类
 * </p>
 *
 * @author ccl
 * @since 2024-11-06
 */
@Slf4j
@Service
public class MjStatisticsServiceImpl extends ServiceImpl<MjStatisticsMapper, MjStatistics> implements MjStatisticsService {

    @Resource
    private MjVisitRecordService mjVisitRecordService;

    @Override
    public boolean add(MjStatistics mjStatistics, HttpServletRequest request) {
        String today=DateUtils.getYear()+DateUtils.getMonth()+DateUtils.getDay();
        LambdaQueryWrapper<MjStatistics> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(MjStatistics::getButtonCode,mjStatistics.getButtonCode())
                .eq(MjStatistics::getType,mjStatistics.getType())
                .eq(MjStatistics::getMenuType,mjStatistics.getMenuType())
                .eq(MjStatistics::getVisitPerson,request.getRemoteAddr())
                .eq(MjStatistics::getNianMonthDay,today);
        MjStatistics mjStatistics1=this.getOne(wrapper);
        if(mjStatistics1==null){
            mjStatistics.setHits(0);
            mjStatistics.setVisits(0);
            if(mjStatistics.getType()==1){
                mjStatistics.setHits(1);
            }
            if(mjStatistics.getType()==2){
                mjStatistics.setVisits(1);
            }
            if(mjStatistics.getType()==3) {
                mjStatistics.setStopTime(mjStatistics.getStopTime()/1000);
            }
            mjStatistics.setNian(DateUtils.getYear());
            mjStatistics.setNianMonth(DateUtils.getYear()+DateUtils.getMonth());
            mjStatistics.setNianMonthDay(DateUtils.getYear()+DateUtils.getMonth()+DateUtils.getDay());
            mjStatistics.setCreateTime(new Date());
            mjStatistics.setUpdateTime(new Date());
            if(ObjectUtil.isEmpty(mjStatistics.getVisitPerson())){
                mjStatistics.setVisitPerson(request.getRemoteAddr());
            }
            this.save(mjStatistics);
        }else{
            //BeanUtil.copyProperties(mjStatistics,mjStatistics1);
            if(mjStatistics.getType()==1){
                mjStatistics1.setHits(mjStatistics1.getHits()+1);
            }
            if(mjStatistics.getType()==2){
                mjStatistics1.setVisits(mjStatistics1.getVisits()+1);
            }
            if(mjStatistics.getType()==3){
                mjStatistics1.setStopTime(mjStatistics1.getStopTime()+mjStatistics.getStopTime());
            }
            mjStatistics1.setUpdateTime(new Date());
            this.updateById(mjStatistics1);
        }
        //页面浏览记录
        if(mjStatistics.getType()==3){
            MjVisitRecord mjVisitRecord=new MjVisitRecord();
            mjVisitRecord.setButtonCode(mjStatistics.getButtonCode());
            mjVisitRecord.setMenuType(mjStatistics.getMenuType());
            mjVisitRecord.setType(mjStatistics.getType());
            mjVisitRecord.setTitleId(mjStatistics.getTitleId());
            mjVisitRecord.setTitleName(mjStatistics.getTitleName());
            mjVisitRecord.setStopTime(mjStatistics.getStopTime());
            mjVisitRecord.setNian(DateUtils.getYear());
            mjVisitRecord.setNianMonth(DateUtils.getYear()+DateUtils.getMonth());
            mjVisitRecord.setNianMonthDay(DateUtils.getYear()+DateUtils.getMonth()+DateUtils.getDay());
            mjVisitRecord.setCreateTime(new Date());
            mjVisitRecord.setUpdateTime(new Date());
            if(ObjectUtil.isEmpty(mjStatistics.getVisitPerson())){
                mjVisitRecord.setVisitPerson(request.getRemoteAddr());
            }else{
                mjVisitRecord.setVisitPerson(mjStatistics.getVisitPerson());
            }


            mjVisitRecordService.save(mjVisitRecord);
        }
        return true;
    }
}
